/*
 * @lc app=leetcode.cn id=747 lang=typescript
 *
 * [747] 至少是其他数字两倍的最大数
 */

// @lc code=start
function dominantIndex(nums: number[]): number {
    if (nums.length === 1) return 0;
    const a0 = nums[0];
    const a1 = nums[1];

    // 第一次更新数据
    let stack = a0 >= a1 ? [a0, a1] : [a1, a0];
    let record = a0 >= a1 ? 0 : 1;

    for (let i = 2; i < nums.length; i++) {
        const element = nums[i];

        console.log(stack);
        if (element < stack[1]) {
            continue;
        }
        if (element > stack[0]) {
            stack = [element, stack[0]];
            record = i;
        } else {
            stack[1] = element;
        }
    }
    let isEnough = stack[0] >= stack[1] * 2;
    if (isEnough) {
        return record;
    } else {
        return -1;
    }
}
// @lc code=end
